﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Card.Web.Member
{
    public partial class SubmitOrder : MemberBase
    {
        #region
        private DataTable m_dtCart;
        #endregion

        #region
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DoInit();
        }
        protected override void OnInit(EventArgs e)
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.rptList.ItemDataBound += new RepeaterItemEventHandler(rptList_ItemDataBound);
            this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
            base.OnInit(e);
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            this.CreateOrder();
        }

        void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                Literal ltlTotalMoney = e.Item.FindControl("ltlTotalMoney") as Literal;
                if (ltlTotalMoney != null && this.m_dtCart != null && this.m_dtCart.Rows.Count > 0)
                {
                    ltlTotalMoney.Text = this.m_dtCart.Compute("SUM(TOTAL_MONEY)", string.Empty).ToString();
                }
            }
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            if (this.DoCheckAddress() && !this.IsRefresh)
            {
                Entity.Address address = address = new Card.Entity.Address();
                address.ID = BAF.BLL.SYSSequenceManager.GetSequence(CONST.SEQUENCE_CODE.ADDRESS_ID, null);
                address.UserID = this.Passport.UserID;
                address.ReceiverName = this.txtReceiver.Text;
                address.Area = this.txtArea.Text;
                address.Street = this.txtStreet.Text;
                address.PostCode = this.txtPostCode.Text;
                address.Phone = this.txtTel.Text;
                if (BLL.AddressManager.Save(address, this.Passport))
                {
                    this.MessageBox(string.Format("{0}成功！", this.btnSave.Text));
                    this.DoInit();
                }

            }
        }
        #endregion

        #region
        private void DoInit()
        {
            this.rptAddress.DataSource = BLL.AddressManager.GetListByUserID(this.Passport.UserID, this.Passport);
            this.rptAddress.DataBind();

            this.rptPaymentMethod.DataSource = Common.LookUpCache.GetLookupValues(CONST.LOOKUP_TYPE.PAYMENT_METHOD);
            this.rptPaymentMethod.DataBind();

            this.rptSippingDate.DataSource = Common.LookUpCache.GetLookupValues(CONST.LOOKUP_TYPE.SIPPING_DATE);
            this.rptSippingDate.DataBind();


            this.m_dtCart = BLL.CartManager.GetListByUserID(this.Passport.UserID);
            if (this.m_dtCart != null && this.m_dtCart.Rows.Count > 0)
            {
                this.rptList.DataSource = this.m_dtCart;
                this.rptList.DataBind();
            }
            else
            {
                this.divEmpty.Visible = true;
                this.divOrder.Visible = false;
            }
        }

        private bool DoCheckAddress()
        {
            if (string.IsNullOrEmpty(this.txtReceiver.Text))
            {
                this.MessageBox("收货人姓名不能为空！");
                return false;
            }

            if (string.IsNullOrEmpty(this.txtArea.Text))
            {
                this.MessageBox("省市区/县不能为空！");
                return false;
            }

            if (string.IsNullOrEmpty(this.txtStreet.Text))
            {
                this.MessageBox("乡镇/街道不能为空！");
                return false;
            }

            if (string.IsNullOrEmpty(this.txtPostCode.Text))
            {
                this.MessageBox("邮政编码不能为空！");
                return false;
            }

            if (string.IsNullOrEmpty(this.txtTel.Text))
            {
                this.MessageBox("联系电话不能为空！");
                return false;
            }

            return true;
        }

        private bool DoCheckSubmitOrder()
        {
            return true;
        }

        private void CreateOrder()
        {
            if (this.DoCheckSubmitOrder())
            {
                Entity.Address address = BLL.AddressManager.GetEntity(int.Parse(this.hndAddress.Value), this.Passport);
                if (address != null)
                {
                    Entity.Order order = new Card.Entity.Order();
                    order.OrderID = Common.CardWebUtility.CreateOrderID();
                    order.UserID = this.Passport.UserID;
                    order.Area = this.Area.SiteCode;
                    order.PaymentMethodCode = this.hndPayment.Value.Trim();
                    order.SippingDateCode = this.hndSippingDate.Value.Trim();
                    order.Remark = this.txtRemark.Text.Trim();
                    order.Receiver = address.ReceiverName;
                    order.PostCode = address.PostCode;
                    order.Address = string.Format("{0} {1}", address.Area, address.Street);
                    order.Phone = address.Phone;
                    order.OrderStatusCode = string.Compare(this.hndPayment.Value.Trim(), "HDFK", true) == 0 ? CONST.ORDER_SATTUS.WAIT_CONFIRM : CONST.ORDER_SATTUS.WAIT_PAY;
                    if (BLL.OrderManager.CreateOrder(order, this.Passport))
                    {
                        Common.CardWebUtility.SetCartInfo(this.Passport.UserID);
                        Response.Redirect(string.Format("ConfirmOrder.aspx?oid={0}", order.OrderID));
                    }
                    else
                    {
                        this.MessageBox("提交失败，请重新提交！");
                    }
                }
                else
                {
                    this.MessageBox("请选择收货信息");
                }
            }
        }
        #endregion
    }
}